<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>自定义材质线</title>
  <link href="src/Cesium1.70/Widgets/widgets.css" rel="stylesheet" />
  <script src="src/Cesium1.70/Cesium.js"></script>
  <script src="src/cesium-materialLine.js"></script>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body,
    #viewer-container {
      width: 100%;
      height: 100%;
      overflow: hidden;
    }

    #btn {
      position: absolute;
      top: 50px;
      left: 50px;
    }
  </style>
</head>

<body>
  <div id="viewer-container"></div>
  <script>
    // init
    function initPage() {
      // 切换自己的token
      Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJlYTQ2ZjdjNS1jM2E0LTQ1M2EtOWM0My1mODMzNzY3YjYzY2YiLCJpZCI6MjkzMjcsInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1OTE5NDIzNjB9.RzKlVTVDTQ9r7cqCo-PDydgUh8Frgw0Erul_BVxiS9c';
      // 初始化
      let viewer = new Cesium.Viewer("viewer-container", {
        infoBox: false,
        shouldAnimate: true,
        vrButton: true,
        geocoder: false,
        homeButton: false,
        sceneModePicker: false,
        baseLayerPicker: true,
        navigationHelpButton: false,
        animation: false,
        timeline: false,
        fullscreenButton: false,
      });

      // 自定义纹理材质
      let MaterialLineImage = [
        './src/line.png',
        './src/line2.png',
        './src/line3.png',
      ]

      // color
      let colors = [
        new Cesium.Color(77 / 255, 201 / 255, 255 / 255, 1),
        new Cesium.Color(255 / 255, 201 / 255, 38 / 255, 1),
        new Cesium.Color(221 / 255, 221 / 255, 221 / 255, 1)
      ];

      // 创建材质线
      let getCustomMaterialLine = (image, color) => {
        return new Cesium.CustomMaterialLine({
          image: image,
          color: color,
          duration: 2000
        })
      }
      // 添加实体线
      let startPoint = Cesium.Cartesian3.fromDegrees(104.081701757991, 30.627042558105988)
      let glowingLine = null
      for (let i = 0, len = 8; i < len; i++) {

        let endPoint = Cesium.Cartesian3.fromDegrees((Math.random() / 100) +
          104.081701757991, (Math.random() / 100) + 30.627042558105988);
        glowingLine = viewer.entities.add({
          polyline: {
            positions: [startPoint, endPoint],
            width: 5,
            material: getCustomMaterialLine(MaterialLineImage[i % 3], colors[i % 3]),
          }
        });
      }

      viewer.flyTo(glowingLine)
    }

    // load
    window.onload = function () {
      initPage()
    }
  </script>
</body>

</html>